package com.example.rocketmq.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.rocketmq.dao.OrderDao;
import com.example.rocketmq.pojo.Order;
import com.example.rocketmq.util.MySendCallback;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.messaging.Message;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
public class OrderController {
    @Autowired
    OrderDao orderDao;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @RequestMapping("/orderlist")
    public String orderlist(Model model){
        return "orderlist";
    }


    /**
     * 商品初始化
     * 可以从数据库查询，并且初始化秒杀商品数量
     */
    @RequestMapping("/init")
    @ResponseBody
    public String init(){
        stringRedisTemplate.opsForValue().set("order","100");
        return "初始化成功当前库存"+100;
    }
    /**
     * 下单  检查库存
     * @param model
     * @return
     */
    @RequestMapping("/createOrder")
    public String createOrder(Model model){
        String order = stringRedisTemplate.opsForValue().get("order");
        if(Integer.parseInt(order)<=0){
            System.out.println("秒杀活动结束");
        }
        model.addAttribute("stock", order);
        return "order";
    }

    /**
     * 提交订单  减库存 并且发送消息到队列
     * @param model
     * @return
     */
    @RequestMapping("/submitOrder")
    public String submitOrder(Model model){
        long order_id = new Date().getTime();

        String order = stringRedisTemplate.opsForValue().get("order");
        if(Integer.parseInt(order)<=0){
            System.out.println("秒杀活动结束");
        }else{
            Long num = stringRedisTemplate.opsForValue().decrement("order", 1);
            if(num<=0){///多线程情况下会超卖的情况，使用一下方法解决
                System.out.println("来晚了，商品已经卖完了");
                stringRedisTemplate.opsForValue().increment("order", 1);
            }else{//将消息发送到mq
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("order_id",order_id);
                jsonObject.put("num",num);
                //发送消费者  用于创建订单
                this.rocketMQTemplate.convertAndSend(
                        "tp_springboot_01",
                        jsonObject.toString()
                );

                Message<String> msg = MessageBuilder.withPayload(order_id+"").build();

                rocketMQTemplate.asyncSend("tp_order_delayed2", msg, new MySendCallback(),2000, 4);
            }
        }
        model.addAttribute("order_id", order_id);
        return "settlement";
    }


    /**
     * 订单支付
     * 改变订单状态
     * @param model
     * @return
     */
    @RequestMapping("/payOrder")
    public String payOrder(String order_id,Model model){
        List<Order> orders = orderDao.queryByOrder_no(order_id);
        if(orders!=null && orders.size()>0){
            orderDao.updateStatusById("1",order_id);
        }
        return "success";
    }


}
